using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using MinesweeperPlus.Scripts.Abstractions.Enums;
using MinesweeperPlus.Scripts.Configs;
using MinesweeperPlus.Scripts.Utils;

namespace MinesweeperPlus.Scripts.UI
{
    public sealed class PopupPanel : UIElement
    {
        public new class UxmlFactory : UxmlFactory<PopupPanel> { }

        #region Actions
        public Action<PopupButtonType> OnButtonClick;
        #endregion

        #region Variables
        private Dictionary<PopupButtonType, Button> _buttons;
        private List<IMGUIContainer> _icons;

        private Label _popupLabel;
        private VisualElement _buttonsPanel;
        #endregion

        #region PopupPanel Methods
        public PopupPanel() : base() { }
        public PopupPanel(
            IconsConfig iconsConfig, 
            StyleSheet styleSheet,
             ColorSchemeConfig colorSchemeConfig) : base(iconsConfig, styleSheet) 
        { 
            ColorSchemeConfig = colorSchemeConfig;
        }

        public void SetPanelType(PopupPanelType popupPanelType)
        {
            switch (popupPanelType)
            {
                case PopupPanelType.Message:
                    ShowButtons(PopupButtonType.Close);
                    break;
                case PopupPanelType.YesOnly:
                    ShowButtons(PopupButtonType.Confirm);
                    break;
                case PopupPanelType.YesOrNo:
                    ShowButtons(PopupButtonType.Confirm, PopupButtonType.Cancel);
                    break;
                case PopupPanelType.Error:
                    ShowButtons(PopupButtonType.Close);
                    break;
            }
        }

        public void ShowButtons(params PopupButtonType[] buttonTypes)
        {
            foreach (var pair in _buttons)
                pair.Value.SetDisplay(false);

            foreach (var buttonType in buttonTypes)
                _buttons[buttonType].SetDisplay(true);
        }

        public void SetPopupText(string text)
        {
            _popupLabel.text = text;
        }

        public void Close()
        {
            this.parent.Remove(this);
        }
        #endregion

        #region Other Methods
        protected override void UpdateColorScheme()
        {
            base.UpdateColorScheme();

            this.SetBackgroundColor(ColorSchemeConfig.CameraBackgroundColor);

            Panel.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
            Panel.SetBorderColor(ColorSchemeConfig.BorderColor);

            _popupLabel.SetTextColor(ColorSchemeConfig.TextColor);

            foreach (var button in _buttons.Values)
            {
                button.SetBackgroundColor(ColorSchemeConfig.BackgroundColor);
                button.SetBorderColor(ColorSchemeConfig.BorderColor);
            }
            foreach (var icon in _icons)
                icon.SetBackgroundImageColor(ColorSchemeConfig.IconColor);
        }

        protected override void CreatePanel()
        {
            this.AddToClassList(Constants.UI_POPUP_CONTAINER_STYLE);
            base.CreatePanel();
            Panel.AddToClassList(Constants.UI_POPUP_PANEL_STYLE);

            _buttons = new();
            _icons = new();

            var closeIcon = CreateIMGUIContainer(IconsConfig.CancelIcon, Constants.UI_SMALL_ICON_STYLE);
            var closeButton = base.CreateButton(
                closeIcon, () => 
                {
                    OnButtonClick?.Invoke(PopupButtonType.Close);
                    Close();
                },
                Constants.UI_POPUP_CLOSE_BUTTON_STYLE);
            _buttons.Add(PopupButtonType.Close, closeButton);
            _icons.Add(closeIcon);

            _popupLabel = new Label();
            _popupLabel.AddToClassList(Constants.UI_SMALL_TEXT_STYLE);
            _popupLabel.AddToClassList(Constants.UI_POPUP_TEXT_STYLE);
            Panel.Add(_popupLabel);
            SetPopupText("text");

            _buttonsPanel = new VisualElement();
            Panel.Add(_buttonsPanel);
            _buttonsPanel.AddToClassList(Constants.UI_POPUP_BUTTONS_PANEL_STYLE);

            CreateButton(
                CreateIMGUIContainer(IconsConfig.ConfirmIcon),
                PopupButtonType.Confirm);
            CreateButton(
                (IMGUIContainer)CreateIMGUIContainer(IconsConfig.NextIcon).Rotate(180),
                PopupButtonType.Back);
            CreateButton(
                (IMGUIContainer)CreateIMGUIContainer(IconsConfig.InfoIcon).Rotate(180),
                PopupButtonType.Info);
            CreateButton(
                CreateIMGUIContainer(IconsConfig.SettingsIcon),
                PopupButtonType.Settings);
            CreateButton(
                CreateIMGUIContainer(IconsConfig.LampIcon),
                PopupButtonType.Hints);
            CreateButton(
                CreateIMGUIContainer(IconsConfig.RestartIcon),
                PopupButtonType.Restart);
            CreateButton(
                CreateIMGUIContainer(IconsConfig.CancelIcon),
                PopupButtonType.Cancel);
        }

        private Button CreateButton(
            IMGUIContainer imguiContainer, 
            PopupButtonType buttonType)
        {
            _icons.Add(imguiContainer);
            var button = base.CreateButton(
                imguiContainer, 
                () => OnButtonClick?.Invoke(buttonType));
            _buttonsPanel.Add(button);
            button.AddToClassList(Constants.UI_POPUP_ELEMENTS_STYLE);
            _buttons.Add(buttonType, button);
            return button;
        }
        #endregion
    }
}
